package com.app.configs;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import javax.xml.stream.*;
import javax.xml.stream.events.*;
import java.util.Enumeration;
import java.util.Properties;


/**
 * Created by Hendra on 12/24/2014.
 */
public abstract class ResConfig extends DataConfigs {
    /** The Constant logger. */
    private final static Logger logger = LoggerFactory.getLogger(ResConfig.class);

    private static final String FILENAME = "cti_config.xml";

    public ResConfig(){
        read();
    }

    @SuppressWarnings({ "unchecked", "null" })
    public void read(){
        try{
            logger.info("Try get data: " + System.getProperty("user.dir") + File.separator + FILENAME);
            FileInputStream fileInputStream = new FileInputStream(new File(System.getProperty("user.dir") +File.separator + FILENAME));
            Properties properties = new Properties();
            properties.loadFromXML(fileInputStream);
            fileInputStream.close();
            Enumeration enuKeys = properties.keys();
            while (enuKeys.hasMoreElements()){
                String key = (String) enuKeys.nextElement();
                String value = properties.getProperty(key);
                if(key.equals(AttributeConfigs.CTI_URL.getAttributeConfig())){
                    setCti_url(value);
                    continue;
                }
                if(key.equals(AttributeConfigs.CTI_PORT.getAttributeConfig())){
                    setCti_port(value);
                    continue;
                }
                if(key.equals(AttributeConfigs.SMTP_INBOUND_SERVER.getAttributeConfig())){
                    setSmtp_inbound_server(value);
                    continue;
                }
                if(key.equals(AttributeConfigs.SMTP_INBOUND_PORT.getAttributeConfig())){
                    setSmtp_inbound_port(value);
                    continue;
                }
                if(key.equals(AttributeConfigs.SMTP_INBOUND_USERNAME.getAttributeConfig())){
                    setSmtp_inbound_username(value);
                    continue;
                }
                if(key.equals(AttributeConfigs.SMTP_INBOUND_PASSWORD.getAttributeConfig())){
                    setSmtp_inbound_password(value);
                    continue;
                }
                if(key.equals(AttributeConfigs.SMTP_EMAIL_LIMIT.getAttributeConfig())){
                    setSmtp_email_limit(value);
                    continue;
                }
            }
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }

    public void save() {}

}
